<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>

    <script type="text/javascript"
            src="http://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
    <script type="text/javascript" src="http://static.robotwebtools.org/roslibjs/current/roslib.min.js"></script>

    <script type="text/javascript" type="text/javascript">
        var log = function () {
            console.log.apply(console, arguments)
        }
        // Connecting to ROS
        // -----------------

        var ros = new ROSLIB.Ros({
            url: 'ws://192.168.1.120:9090'
        });

        ros.on('connection', function () {
            console.log('Connected to websocket server.');
        });

        ros.on('error', function (error) {
            console.log('Error connecting to websocket server: ', error);
        });

        ros.on('close', function () {
            console.log('Connection to websocket server closed.');
        });

        // Publishing a Topic
        // ------------------

        var cmdVel = new ROSLIB.Topic({
            ros: ros,
            name: '/cmd_vel',
            messageType: 'geometry_msgs/Twist'
        });

        log("cmdVel", cmdVel, typeof cmdVel)

        _cmdVel = JSON.stringify(cmdVel)
        log("_cmdVel", _cmdVel, typeof _cmdVel)


        var twist = new ROSLIB.Message({
            linear: {
                x: 0.1,
                y: 0.2,
                z: 0.3
            },
            angular: {
                x: -0.1,
                y: -0.2,
                z: -0.3
            }
        });

        log("twist", twist, typeof twist)

        var result = cmdVel.publish(twist);

        log("cmdVel.publish(twist)", result)

        // Subscribing to a Topic
        // ----------------------

        var listener = new ROSLIB.Topic({
            ros: ros,
            name: '/listener',
            messageType: 'geometry_msgs/Twist'
        });

        log('listener', listener, typeof listener)

        var result_sub = listener.subscribe(function (message) {
            console.log('Received message on ' + listener.name + ': ' + message);
            listener.unsubscribe();
        });

        log('listener.subscribe', result_sub)
        //
        // // Calling a service
        // // -----------------
        //
        // var addTwoIntsClient = new ROSLIB.Service({
        //     ros: ros,
        //     name: '/add_two_ints',
        //     serviceType: 'rospy_tutorials/AddTwoInts'
        // });
        //
        // var request = new ROSLIB.ServiceRequest({
        //     a: 1,
        //     b: 2
        // });
        //
        // addTwoIntsClient.callService(request, function (result) {
        //     console.log('Result for service call on '
        //         + addTwoIntsClient.name
        //         + ': '
        //         + result.sum);
        // });
        //
        // // Getting and setting a param value
        // // ---------------------------------
        //
        // ros.getParams(function (params) {
        //     console.log(params);
        // });
        //
        // var maxVelX = new ROSLIB.Param({
        //     ros: ros,
        //     name: 'max_vel_y'
        // });
        //
        // maxVelX.set(0.8);
        // maxVelX.get(function (value) {
        //     console.log('MAX VAL: ' + value);
        // });
    </script>
</head>

<body>
<h1>Simple roslib Example</h1>
<p>Check your Web Console for output.</p>
</body>
</html>